package com.qsl.system.controller;


import com.qsl.common.result.Result;
import com.qsl.model.system.SysMenu;
import com.qsl.model.vo.AssginMenuVo;
import com.qsl.system.annotation.LogAnnotation;
import com.qsl.system.enums.BusinessType;
import com.qsl.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author qsl
 * @since 2023-03-29
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/sysMenu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("查询菜单-树形")
    @GetMapping("findNodes")
    public Result findNodes() {
        List<SysMenu> list = sysMenuService.findNodes();
        return Result.ok(list);
    }

    @ApiOperation("修改状态")
    @PostMapping("updateState/{Id}/{status}")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.UPDATE) // 自定义注解-操作日志—新增
    public Result updateState(@PathVariable String Id, @PathVariable Integer status) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(Id);
        sysMenu.setStatus(status);
        boolean flag = sysMenuService.updateById(sysMenu);
        if (flag) {
            return Result.ok(flag);
        } else {
            return Result.fail(flag);
        }
    }


    @ApiOperation("新增")
    @PostMapping("save")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.INSERT) // 自定义注解-操作日志—新增
    public Result save(@RequestBody SysMenu sysMenu) {
        sysMenuService.save(sysMenu);
        return Result.ok();
    }

    @ApiOperation("修改-根据id查询")
    @GetMapping("update/{Id}")
    public Result getByMenu(@PathVariable String Id) {
        SysMenu sysMenu = sysMenuService.getById(Id);
        return Result.ok(sysMenu);
    }

    @ApiOperation("修改")
    @PostMapping("update")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.UPDATE) // 自定义注解-操作日志—新增
    public Result update(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.ok();
    }


    @ApiOperation("删除")
    @DeleteMapping("delete/{Id}")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.DELETE) // 自定义注解-操作日志—新增
    public Result delete(@PathVariable String Id) {
        sysMenuService.removeMenuById(Id);
        return Result.ok();
    }

    //   =================权限==============
    @ApiOperation("根据角色获取菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId) {
        List<SysMenu> list = sysMenuService.findSysMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @ApiOperation("给角色分配权限")
    @PostMapping("doAssign")
    @LogAnnotation(title = "角色管理", businessType = BusinessType.INSERT) // 自定义注解-操作日志—新增
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysMenuService.doAssign(assginMenuVo);
        return Result.ok();
    }
}

